/*
 *  Copyright 2011 Iurii Dunko, Iana Potochniak.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.yyminijava.syntaxtree;

import org.opu.yyminijava.visitor.Visitor;

/**
 *  Call {@link #initialisationComplete} after you build this object.
 * @author Iurii Dunko
 * @author Iana  Potochniak
 */
public class ArrayType extends ReferenceType {

    private static final long serialVersionUID = 1323110697103L;

    private Type type;
    /** The array dimension (it's must be bigger then 0 */
    private int dimension = -1;

    public ArrayType() {
    }

    public ArrayType(Type type) {
        this.type = type;
    }

    public ArrayType(Type type, int dimension) {
        this.type = type;
        this.dimension = dimension;
    }

    public Type getType() {
        return type;
    }

    public void setType(Type type) {
        validate(type);
        this.type = type;
    }

    public int getDimension() {
        return dimension;
    }

    public void setDimension(int dimension) {
        this.dimension = dimension;
    }

    @Override
    public String getName() {
        return type.getName();
    }

    /**
     * Validate type and dimension of current array.
     */
    public void initialisationComplete(){
        validate(type);
        validate(dimension);
    }

    private void validate(Type type) {
        if ((type == null)
                || !((type instanceof PrimitiveType) || (type instanceof ClassType))){
            // TODO: create own exception struction!
            // Using error is temporary solution, we use exception when the
            // incoming file contains errors, when the problem in our compiler
            // we use errors (but it for now)
            throw new Error("Illegal array type: " + type.getClass());
        }
    }

    private void validate(int size){
        if (size != 1){
            // TODO: create own exception struction!
            // Using error is temporary solution, we use exception when the
            // incoming file contains errors, when the problem in our compiler
            // we use errors (but it for now)
            throw new Error("Current version support only dimensional array");
        }
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final ArrayType other = (ArrayType) obj;
        if (this.type != other.type && (this.type == null || !this.type.equals(other.type))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 17 * hash + (this.type != null ? this.type.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return String.valueOf(type) + "[]";
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(type);
    }
}
